package com.example.administrator.notification;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

import java.text.SimpleDateFormat;
import java.util.Date;


public class NotifyService extends Service {
    private NotificationManager nm;
    private boolean isRec = false;
    private boolean isFirst = true;
    private String ACTION = Intent.ACTION_TIME_TICK;
    private SharedPreferences sp;
    private IntentFilter ifter;
    private TimeReceiver receiver;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        sp = getSharedPreferences("UserNote", MODE_PRIVATE);
        nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        ifter = new IntentFilter();
        ifter.addAction(ACTION);
        receiver = new TimeReceiver();
        if (isRec == false){
            registerReceiver(receiver, ifter);
            isRec = true;
        }
        Log.i("Service","onCreate");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        System.out.println("服务结束");
        unregisterReceiver(receiver);
    }

    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("Service","onStart");
        if (isFirst) {
            isFirst = false;
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy:MM:dd:HH:mm");
            String curTime = sdf.format(new Date());
            System.out.println(curTime);

            String time = sp.getString("time", "0000");
            System.out.println("curTime" + curTime + "setTime" + time);
            if (curTime.equals(time)) {
                String text = sp.getString("text", "默认事件");
                System.out.println("onStart");
                PendingIntent pi = PendingIntent.getActivity(this,0, new Intent(this,MainActivity.class),0);
                Notification.Builder builder = new Notification.Builder(this);
                builder.setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle("小贴士")
                        .setContentText(text)
                        .setContentIntent(pi)
                        .setAutoCancel(true);
                nm.notify(1044,builder.build());
                Intent ActIntent = new Intent(this, DialogActivity.class);
                ActIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(ActIntent);
                stopSelf();
            }
        }
        return super.onStartCommand(intent, flags, startId);
    }
}
